<?php
	header("Content-Type: text/html; charset=iso-8859-1");

	session_start();
	if(!$_SESSION['logged']){
		header("Location: index.php");
		exit;
	}

    require_once('include/db_connection.php');
    require_once('include/script_includes.php');
    require_once('include/config.inc.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MMS-Dart-Liga</title>
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/>
        <link rel="stylesheet" type="text/css" href="css/global.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery.jqplot.min.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery-ui.css"/>
		<link rel="stylesheet" type="text/css" href="css/jquery.contextMenu.css"/>
		<link rel="stylesheet" type="text/css" href="dist/themes/default/style.min.css" />

        <script type="text/javascript" src="scripts/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="scripts/jquery-ui.js"></script>
		<script type="text/javascript" src="scripts/jquery.ui.position.js"></script>
		<script type="text/javascript" src="scripts/jquery.contextMenu.js"></script>
		<script type="text/javascript" src="scripts/jquery.windowmsg-1.0.js"></script>
		<script type="text/javascript" src="scripts/jquery.hoverIntent.minified.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jquery.jqplot.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.highlighter.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.cursor.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dateAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasTextRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasAxisTickRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.categoryAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.BezierCurveRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.trendline.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dragable.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.pieRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.ohlcRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.barRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.pointLabels.min.js"></script>
        <script type="text/javascript" src="scripts/main.js"></script>
        <script type="text/javascript" src="scripts/dartautomat.js"></script>
		<script type="text/javascript" src="dist/jstree.min.js"></script>
		<script type="text/javascript" src="scripts/jquery.iosslider.js"></script>

    </head>
    <body>
        <div id="pageWrapper">
            <div id="pageHeader">

            </div>
            <div id="contentWrapper">
                <div id="contentHeader">
                    <div id="siteTitle">
                        <h1>MMS-Dart-Liga</h1>
						<?php echo "<div class='user'><a href='logout.php'>".$_SESSION['fname']." ".$_SESSION['lname']." (".$_SESSION['role'].")</a> <a href='statistics.php' onclick='goStatistics(this);'>Dashboard</a></div>";
                            /*require_once('include/navi.php');*/
                        ?>
                    </div>
                    <div id="navi">
						
						<div class='iosSlider'>
							<div class='slider'>
								<div class = 'item'>News</div>
								<div class = 'item'>Dartz�hler: 
									<?php
										$sql = "select count(*) count from game_score_v2";
										print($mysqli->query($sql)->fetch_object()->count);
									?>
								</div>
								<div class = 'item'>Top Score: <?=getTopScore()?></div>
								<div class = 'item'>Top 3-Dart-Average: <?=getTopAvg()?></div>
							</div>
						</div>
						<div class="statisticLayer"><div style='float:right;display:block;width:250px;height:200px;margin:20px;' id='chartOverlay1'></div><div style='display:block;width:330px;height:200px;margin:20px;' id='chartOverlay2'></div></div>
						<div id='livelink'></div>
                    </div>
                </div>
                <div id="content">
                    <div class="contentLeft">
                        <?php
                    // save game
					$rndGame = rand(1, 20);
					if(isset($_GET['playerid'])) {
						$players = $_GET['playerid'];
					} else {
						$players = null;
					}
                    game_save($mysqli,null,$_SESSION['role'],$players,$rndGame);

                    if (isset($_GET['savegame']) == true) {
                        $res = $mysqli->query("select max(id) max_game_id from game_data");
                        $row = $res->fetch_assoc();
                        $id = $row['max_game_id'] + 1;
                        foreach ($_GET as $key => $value) {
                            if (startsWith($key, 'player_') && $value != "") {
                            	$tmpArr = explode("_", $key);
                                $player_id = $tmpArr[1];
                                $sql = "INSERT INTO game_data (id,date, game_id, player_id, score) VALUES (" . $id . ",CURDATE(), " . $_GET['game_id'] . "," . $player_id . "," . $value . ")";
                                mysqli_query($mysqli, $sql);
                                $mysqli->commit();
                            }
                        }
                    }
                        ?>
                    </div>
                    <div class="contentMiddle">
                        <?php

                            if (isset($_GET['kw']) == true) {
								$yearkw = $_GET['kw'];
								games_kw_summary($mysqli, $yearkw, $yearkw);
							} else {
								$yearkw = date('Y') . ltrim( date('W'),'0');
								games_kw_summary($mysqli, $yearkw, $yearkw);
							}
							getRandomGame($mysqli,$_SESSION['role'],null);
                        ?>
                    </div>
                    <div class="contentRight">
                        <?php
							getTopScores($mysqli,$_SESSION['role']);
							getTopAvgs($mysqli,$_SESSION['role']);
							
                            $kwWinners = getKwWinners(27);
                            if (is_array($kwWinners) and count($kwWinners) > 0) {
                        ?>
							<div class="kwWinners scrollbarover">
								<table class="gridtable">
									<tr>
										<th colspan="3">Wochensieger</th>
									</tr>
									<?php
									foreach($kwWinners as $kwWinner) {
										if (is_array($kwWinner) and count($kwWinner) > 0) {
											foreach($kwWinner as $userTemp) {
												if (isset($userTemp['player_id'])) {
												?>
												<tr>
													<td class="color_<?=$userTemp['player_id']?>"><?= $userTemp['firstname']. " " . $userTemp['lastname']?></td>
													<td><?= $userTemp['kw']. " - " . $userTemp['year']?></td>
													<td><?= round($userTemp['score_abs'], 2);?></td>
												</tr>
												<?php
												}
											}
										}
									}
									?>
									</table>
								</div>
                                <?php
                            }
                        ?>
                    </div>
                    <div class="clear"></div>

                    <div class="contentFull">

                    <?php
                        if (isset($_GET['kw']) == true) {
                            $yearkw = $_GET['kw'];
                            games_table($mysqli, $yearkw, null, null);
                        } else {
                            $yearkw = date('Y') . date('W');
                            games_table($mysqli, $yearkw, null, null);
                        }

                        if (isset($_GET['debug']) == true) {
                            foreach ($_GET as $key => $value) {
                                echo "\$_GET['$key'] = $value<br/>";
                            }
                        }
                    ?>

                    </div>
                </div>
            </div>
            <div id="pageFooter">
			</div>
            <div id="statisticsDialog"></div>
        </div>
    </body>
</html>